<?php

namespace App\Http\Services;

use App\Http\Model\ActivityMall;
use App\Http\Model\ActivityPearl;
use App\Http\Model\AudioApi;
use App\Http\Model\Config;
use App\Http\Model\Gift;
use App\Http\Model\NationFlag;
use App\Http\Model\NationGrid;
use App\Http\Model\NationRecharge;
use App\Http\Model\NationSendGift;
use App\Http\Model\Room;

class NationServices extends CommonServices
{
    protected $configModel;
    protected $rechargeModel;
    protected $audioApi;
    protected $activityPeral;
    protected $activityMall;
    protected $sendGiftModel;
    protected $roomModel;
    protected $giftModel;
    protected $nationFlagModel;
    protected $nationGridModel;

    public function __construct()
    {
        $this->configModel = new Config();
        $this->rechargeModel = new NationRecharge();
        $this->audioApi = new AudioApi();
        $this->activityMall = new ActivityMall();
        $this->activityPeral = new ActivityPearl();
        $this->sendGiftModel = new NationSendGift();
        $this->giftModel = new Gift();
        $this->roomModel = new Room();
        $this->nationFlagModel = new NationFlag();
        $this->nationGridModel = new NationGrid();
    }


    /*
    * 查看是否已经有充值记录
    * @param [$user_id]   用户id
    * @param [$rank]   充值等级
    * */
    public function checkRecharge($user_id, $rank)
    {
        //查看用户当天充值记录
        $today_log = $this->rechargeModel->getLogToRank($user_id, $rank);
        if ($today_log) {
            //存在该等级记录
            return false;
        }
        //生成记录
        return $this->rechargeModel->createLog($user_id, $rank);
    }


    //获取用户礼包记录
    public function getRechargeLog($user_id)
    {
        $all_rank = [1, 2, 3];
        $log = $this->rechargeModel->getLogByUserId($user_id, ['id', 'user_id', 'rank', 'status']);
        $count = count($log);
        if ($count == 3) {
            return $log;
        }
        $all_log = [];
        $rank = array_column($log, 'rank');
        $diff_arr = array_diff($all_rank, $rank);
        foreach ($diff_arr as $k => $v) {
            $all_log[$k]['id'] = 0;
            $all_log[$k]['user_id'] = (int)$user_id;
            $all_log[$k]['rank'] = $v;
            $all_log[$k]['status'] = 0;
        }
        $arr = array_merge($log, $all_log);
        array_multisort(array_column($arr, 'rank'), SORT_ASC, $arr);
        return $arr;
    }


    //点击领取
    public function giftBagClickGet($user_id, $rank)
    {
        $res = $this->rechargeModel->getLogToRank($user_id, $rank);
        $code = 200;//
        $msg = '成功';//
        $data = [];//
        do {
            $config = $this->configModel->getShareGoodsConfig(6);
            $activity_date = explode(' - ', $config['nation_date']['value']);
            if (date('Y-m-d H:i:s', time()) > $activity_date[1]) {
                $code = 406;
                $msg = '活动过期';
                break;
            }
            if (!$res) {
                $code = 201;//未充值该等级
                $msg = '请充值后领取';//未充值该等级
                break;
            }
            if ($res->status == 1) {
                $code = 202;//该礼包已领取
                $msg = '您已领取该礼包';//该礼包已领取
                break;
            }
            $api_msg = '国庆活动赠送';
            $time_limit = 3;
            $gift_log = [];
            switch ($rank) {
                case 1:
                    $pearl = mt_rand(12, 60);
                    $this->audioApi->pearl_recharge($user_id, $pearl, $api_msg);
                    $gift_log['type'] = 0;
                    $gift_log['value'] = $pearl;
                    break;
                case 2:
                    $radio_arr = [0.5, 0.3, 0.2];
                    $radio = aliasMethodRand($radio_arr) ?? 0;
                    switch ($radio) {
                        case 0:  //金豆
                            $pearl = mt_rand(60, 300);
                            $gift_log['type'] = 0;
                            $gift_log['value'] = $pearl;
                            $this->audioApi->pearl_recharge($user_id, $pearl, $api_msg);
                            break;
                        case 1:
                            $goods_id = 40; //年年有余
                            $goods_type = 1;
                            $gift_log['type'] = $goods_type;
                            $gift_log['value'] = $goods_id;
                            $this->audioApi->sendUsedMall($user_id, $goods_id, $goods_type, $time_limit);
                            break;
                        case 2:
                            $goods_id = 30; //王校长
                            $goods_type = 3;
                            $gift_log['type'] = $goods_type;
                            $gift_log['value'] = $goods_id;
                            $this->audioApi->sendUsedMall($user_id, $goods_id, $goods_type, $time_limit);
                            break;
                    }
                    break;
                case 3:
                    $radio_arr = [0.4, 0.2, 0.35, 0.05];
                    $radio = aliasMethodRand($radio_arr) ?? 0;
                    switch ($radio) {
                        case 0:  //金豆
                            $pearl = mt_rand(196, 980);
                            $gift_log['type'] = 0;
                            $gift_log['value'] = $pearl;
                            $this->audioApi->pearl_recharge($user_id, $pearl, $api_msg);
                            break;
                        case 1:
                            $goods_id = 48; //小肥猫
                            $goods_type = 1;
                            $gift_log['type'] = $goods_type;
                            $gift_log['value'] = $goods_id;
                            $this->audioApi->sendUsedMall($user_id, $goods_id, $goods_type, $time_limit);
                            break;
                        case 2:
                            $goods_id = 38; //小柯基
                            $goods_type = 3;
                            $gift_log['type'] = $goods_type;
                            $gift_log['value'] = $goods_id;
                            $this->audioApi->sendUsedMall($user_id, $goods_id, $goods_type, $time_limit);
                            break;
                        case 3:
                            $goods_id = 57; //秋名山车神
                            $goods_type = 2;
                            $gift_log['type'] = $goods_type;
                            $gift_log['value'] = $goods_id;
                            $this->audioApi->sendUsedMall($user_id, $goods_id, $goods_type, $time_limit);
                            break;
                    }
                    break;
            }
            if ($gift_log['type'] == 0) {
                $this->activityPeral->create($user_id, $gift_log['value'], 1, 1, 2);
            } else {
                $this->activityMall->create($user_id, $gift_log['value'], $gift_log['type'], $time_limit, 1, 1);
            }
            $data = $gift_log;
            $gift_log = json_encode($gift_log);
            $updateRank = $this->rechargeModel->updateLog($user_id, $rank, $gift_log);
            if (!$updateRank) {
                $code = 203;//领取失败
                $msg = '领取失败';//领取失败
                break;
            }
        } while (false);
        return compact('code', 'msg', 'data');
    }

    public function collectVeil($to_user_ids, $gift_id, $gift_num)
    {
        $num = count($to_user_ids) * $gift_num ?? 0;
        if ($num == 0) {
            return false;
        }
        //获取用户赠送记录
        $veil_num = $kiss_num = 0;
        foreach ($to_user_ids as $v) {
            switch ($gift_id) {
                case 69: //婚纱
                    $veil_num = $num;
                    break;
                case 59://kis
                    $kiss_num = $num;
                    break;
            }
            $send_log = $this->sendGiftModel->getLogByUserId($v);
            if (!$send_log) {
                //无记录创建
                $log = $this->sendGiftModel->createLog($v, $veil_num, $kiss_num);
                if (!$log) {
                    write_log('nationConsumer', 'createSendGiftError', $v . '-' . $veil_num . '-' . $kiss_num);//记录用户礼物 错误
                }
                break;
            }
            //修改用户礼物数量
            $inc = $this->sendGiftModel->updateLog($v, $veil_num, $kiss_num);
            if (!$inc) {
                write_log('nationConsumer', 'IncSendGiftError', $v . '-' . $veil_num . '-' . $kiss_num);//修改用户礼物 错误
            }
            $ring = $this->sendGiftModel->updateRing($v);
            if (!$ring) {
                write_log('nationConsumer', 'ringStatusError', $v . '-' . $veil_num . '-' . $kiss_num);//戒指状态修改 错误
            }
            break;
        }
    }

    //礼物信息
    public function giftRank($gift_id, $user_id, $to_user_ids, $room_id, $gift_num)
    {
        $gift_info = $this->giftModel->getGiftInfo($gift_id);
        if (!$gift_info) {
            write_log('nationConsumer', 'giftInfoError', $gift_id);//记录礼物 错误
            return false;
        }
        $collect_sorce = $gift_info['price'];
        $send_sorce = $collect_sorce * count($to_user_ids) * $gift_num; //用户送出积分
        return $this->sendGiftModel->rankCache($user_id, $to_user_ids, $room_id, $send_sorce, $collect_sorce);
    }

    public function clickRing($id)
    {
        $ring_id = 11; //戒指id
        //获取用户
        $ring = $this->sendGiftModel->getLogByUserId($id);
        $code = 200;
        $msg = '成功';
        $data = [];
        do {
            $config = $this->configModel->getShareGoodsConfig(6);
            $activity_date = explode(' - ', $config['nation_date']['value']);
            if (date('Y-m-d H:i:s', time()) > $activity_date[1]) {
                $code = 406;
                $msg = '活动过期';
                break;
            }
            if (!$ring || $ring['status'] == 0) {
                $code = 201;
                $msg = '您还不可领取荣誉戒指';
                break;
            }
            if ($ring['is_ring'] == 1) {
                $code = 202;
                $msg = '您已经领取过荣誉戒指';
                break;
            }
            $res = $this->sendGiftModel->clickRing($id);
            if (!$res) {
                write_log('nationConsumer', 'clickRingError', $id);//记录礼物 错误
                $code = 203;
                $msg = '领取失败，请重试';
                break;
            }
            $this->activityMall->create($id, $ring_id, 5, 0, 1, 1);
            $api = $this->audioApi->sendRing($id, $ring_id, 0);
            if (!$api) {
                write_log('nationConsumer', 'apiSendRingError', $id);//记录礼物 错误
            }
        } while (false);
        return compact('code', 'msg', 'data');
    }


    public function getSendGiftLog($user_id)
    {
        $data['veil_num'] = $data['kiss_num'] = $data['is_ring'] = $data['status'] = 0;
        $res = $this->sendGiftModel->getLogByUserId($user_id);
        if ($res) {
            $data['veil_num'] = $res['veil_num'];
            $data['kiss_num'] = $res['kiss_num'];
            $data['is_ring'] = $res['is_ring'];
            $data['status'] = $res['status'];
        }
        return $data;
    }

    //兑换亲密值
    public function convert($user_id, $type = 1, $num = 1)
    {
        $code = 200;
        $msg = '成功';
        do {
            $config = $this->configModel->getShareGoodsConfig(6);
            $activity_date = explode(' - ', $config['nation_date']['value']);
            if (date('Y-m-d H:i:s', time()) > $activity_date[1]) {
                $code = 406;
                $msg = '活动过期';
                break;
            }
            //查看用户玉兔
            $user_fowl_num = $this->nationFlagModel->getFlag($user_id);
            if (!$user_fowl_num) {
                $code = 404;
                $msg = '你还未获得小旗，请重试';
                break;
            }

            if ($user_fowl_num->real_num < $num) {
                $code = 404;
                $msg = '您小旗数量不足，请重试';
                break;
            }

            $intimacy_vaule = 0;
            $api = true;
            switch ($type) {
                case 1:
                    //验证cp关系
                    $user_cp = $this->nationFlagModel->getUserCp($user_id);
                    if (!$user_cp) {
                        $code = 203;
                        $msg = '您还没有cp，不可以兑换亲密值';
                        break;
                    }
                    if ($user_fowl_num->consumer_intimacy_num + $num > 100) {
                        $code = 410;
                        $msg = '您的兑换亲密值已超过100点';
                        break;
                    }
                    $api = $this->audioApi->sendIntimacyValue($user_id, $user_cp['cp_user_id'], $num);
                    $this->activityPeral->create($user_id, $num, 1, 3, 3);
                    $intimacy_vaule = $num;
                    break;
                case 2:
                    $goods_id = 112;
                    $goods_type = 4;
                    $time_limit = 3;
                    $api = $this->audioApi->sendUsedMall($user_id, $goods_id, $goods_type, $time_limit);
                    $intimacy_vaule = 1;
                    $this->activityMall->create($user_id, $goods_id, $goods_type, $time_limit, 1, 2);
                    break;
            }
            if (!$api) {
                write_log('nationConsumer', 'convertMallError', [$user_id, $user_id, 'num' => $num, 'type' => $type]);
                return false;
            }

            //减少用户可用玉兔数量
            $this->nationFlagModel->updateRabbitToConvert($user_id, $intimacy_vaule, $type);
        } while (false);

        return ['code' => $code, 'msg' => $msg];
    }

    public function showUserIntimacy($user_id)
    {
        $user_fowl_num = $this->nationFlagModel->getFlag($user_id);
        return $user_fowl_num;
    }


    public function payTicket($user_id, $num)
    {
        if ($num < 1) {
            write_log('nationConsumer', 'payTicketNumError', [$user_id, $user_id, 'num' => $num]);
            return false;
        }
        //验证用户是否存在购买记录
        $pay_log = $this->nationGridModel->getInfo($user_id);
        if (!$pay_log) {
            $create_log = $this->nationGridModel->createTicket($user_id, $num);
            if (!$create_log) {
                write_log('nationConsumer', 'createTicketNumError', [$user_id, $user_id, 'num' => $num]);
                return false;
            }
        } else {
            //增加券
            $inc = $this->nationGridModel->incNum($user_id, $num);
            if (!$inc) {
                write_log('nationConsumer', 'incTicketNumError', [$user_id, $user_id, 'num' => $num]);
                return false;
            }
        }
        $this->nationGridModel->buyTicketLog($user_id, $num);
        write_log('nationConsumer', 'ticketNumSuccess', [$user_id, $user_id, 'num' => $num]);
        return true;
    }


    public function checkTicket($user_id, $num)
    {
        $log = $this->nationGridModel->getInfo($user_id);
        $code = 200;
        $msg = '成功';
        $data = [];
        do {
            $config = $this->configModel->getShareGoodsConfig(6);
            $activity_date = explode(' - ', $config['nation_date']['value']);
            if (date('Y-m-d H:i:s', time()) > $activity_date[1]) {
                $code = 406;
                $msg = '活动过期';
                break;
            }
            if (!$log) {
                $code = 201;
                $msg = '您还未获得抽奖券';
                break;
            }
            if ($log['ticket_num'] < $num) {
                $code = 202;
                $msg = '抽奖券不足，请重试';
                break;
            }
            // 用户消耗抽奖券
            $consumer_ticket = $this->nationGridModel->decrementNum($user_id, $num);
            if (!$consumer_ticket) {
                write_log('nationConsumer', 'ticketError', ['user_id' => $user_id, 'num' => $num]);//抽奖券使用错误
                $code = 203;
                $msg = '抽奖券使用失败，请重试';
                break;
            }
            $radio_arr = [
                '0.115',//普                0
                '0.0045',//摩托车 1天       1
                '0.1576',//积分100          2
                '0.081',//天                 3
                '0.0001',//靓号               4
                '0.0033',//亲密值3点            5
                '0.0655',//同                 6
                '0.003',//法拉利1天           7
                '0.04',//金豆                 8
                '0.01',//庆                9
                '0.5',//积分 50                10
                '0.02',//亲密值 1点             11
            ];
            //获得随机概率
            $gift_arr = $this->getGridAward($radio_arr, $num, $user_id);
            if (empty($gift_arr)) {
                $code = 203;
                $msg = '抽奖失败，请重试';
                break;
            }
            //转换同等类型商品
            $gift_count = array_count_values($gift_arr);
            $user_cp = $this->nationFlagModel->getUserCp($user_id); //查看用户cp
            $moto_id = 89; //摩托
            $cat_id = 34;//法拉利
            $mall_type = 2;//商品类型 2座驾
            $insert = [];
            //0 普  1摩托 2 100积分 3 天 4 靓号 5亲密值3点 6庆 7法拉利 8金豆  9同 10积分50 11 亲密值 1 点

            foreach ($gift_count as $k => $v) {
                $insert[$k]['user_id'] = $user_id;
                $insert[$k]['gift_type'] = $k;
                $insert[$k]['num_count'] = $v;
                $insert[$k]['gift_value'] = $v;
                switch ($k) {
                    case 0://普
                        break;
                    case 1://摩托
                        $this->audioApi->sendUsedMall($user_id, $moto_id, $mall_type, $v);
                        break;
                    case 2://100积分
                        $insert[$k]['gift_value'] = $v * 100;
                        break;
                    case 3://天
                        break;
                    case 4://靓号
                        $liang = '';
                        for ($l_i = 0; $l_i < $v; $l_i++) {
                            $liang_num = $this->nationGridModel->popLiang();
                            if (!empty($liang_num)) {
                                $liang .= $liang_num . ',';
                                $liang_msg = '恭喜您获得靓号:' . $liang_num;
                                $this->audioApi->sendMsgApi($user_id, $liang_msg);
                            }
                        }
                        $insert[$k]['gift_value'] = rtrim($liang, ',');
                        break;
                    case 5://亲密值3点
                        $insert[$k]['gift_value'] = $v * 3;
                        if ($user_cp) {
                            $this->audioApi->sendIntimacyValue($user_id, $user_cp['cp_user_id'], $v * 3);
                        }
                        break;
                    case 6://同
                        break;
                    case 7://法拉利
                        $this->audioApi->sendUsedMall($user_id, $cat_id, $mall_type, $v);
                        break;
                    case 8://金豆
                        $rand = rand(249, 300) * $v;
                        $insert[$k]['gift_value'] = $rand;
                        $this->audioApi->pearl_recharge($user_id, $rand, '钻石抽奖赠送');
                        break;
                    case 9: //庆
                        break;
                    case 10://50积分
                        $insert[$k]['gift_value'] = $v * 50;
                        break;
                    case 11:
                        //亲密值1点
                        if ($user_cp) {
                            $this->audioApi->sendIntimacyValue($user_id, $user_cp['cp_user_id'], $v);
                        }
                        break;
                }
            }
            //新增中奖纪录
            $insertBatch = $this->nationGridModel->insertAllData($insert);
            if (!$insertBatch) {
                write_log('dialTicket', 'insertBatchError', $insert);//记录生成失败
                return false;
            }
            write_log('dialTicket', 'ticketSuccess', ['user_id' => $user_id, 'num' => $num]);//抽奖券使用成功
            $data['insert_data'] = array_values($insert);
            foreach ($gift_arr as &$v) {
                $v = $v + 1;
            }
            $data['gift_arr'] = array_values($gift_arr);
            break;
        } while (false);
        return ['code' => $code, 'msg' => $msg, 'data' => $data];
    }

    //普天同庆兑换旗子
    public function editFeildNum($user_id)
    {
        $res = $this->nationGridModel->getMalllog($user_id, [0, 3, 6, 9]);
        $code = '200';
        $msg = '成功';
        $consumer_flg = $this->nationGridModel->getConvertFieldFlag($user_id);
        do {
            $config = $this->configModel->getShareGoodsConfig(6);
            $activity_date = explode(' - ', $config['nation_date']['value']);
            if (date('Y-m-d H:i:s', time()) > $activity_date[1]) {
                $code = 406;
                $msg = '活动过期';
                break;
            }
            if (count($res) < 4) {
                $code = '201';
                $msg = '不足兑换';
                break;
            }
            $res = array_column($res, 'num_count');
            $min_num = min($res);
            if ($min_num <= $consumer_flg) {
                $code = '201';
                $msg = '旗帜数量不足';
                break;
            }
            //获取用户旗子记录
            $log = $this->nationFlagModel->getFlag($user_id);
            if (!$log) {
                $log = $this->nationFlagModel->create($user_id);
            }

            //添加兑换记录
            $flag = $min_num - $consumer_flg;
            $consumer_flg = $this->nationGridModel->createConvertFieldFlag($user_id, $flag);
            if (!$consumer_flg) {
                $code = '201';
                $msg = '兑换失败';
                break;
            }

            $this->nationFlagModel->updateUserFlag($user_id, $flag);
            break;
        } while (false);
        return ['code' => $code, 'msg' => $msg];
    }

    //获取普天同庆
    public function getFlagFeildNum($user_id)
    {
        $gift_all_type = [0, 3, 6, 9];
        $res = $this->nationGridModel->getMalllog($user_id, [0, 3, 6, 9]);
        $consumer_flg = $this->nationGridModel->getConvertFieldFlag($user_id);

        $gift_type = array_column($res, 'gift_type');
        $data = [];
        foreach ($gift_all_type as $k => $v) {
            if (!in_array($v, $gift_type)) {
                $data[$k]['user_id'] = (int)$user_id;
                $data[$k]['gift_type'] = $v;
                $data[$k]['num_count'] = 0;
            }
        }
        $data = array_values($data);
        $arr = array_merge($res, $data);
        array_multisort(array_column($arr, 'gift_type'), SORT_ASC, $arr);
        foreach ($arr as &$v) {
            $v['num_count'] = $v['num_count'] - $consumer_flg;
        }
        $min_count = array_column($arr, 'num_count');
        $ret['feild_num'] = $arr; //字 数量
        $ret['flag_num'] = min($min_count);//最少旗子
        return $ret;
    }

    //获取抽奖次数信息
    public function getGridNum($user_id)
    {
        $data['all_num'] = 0;
        $data['ticket_num'] = 0;
        $data['award_score'] = 0;
        $ticket = $this->nationGridModel->getInfo($user_id);
        if (empty($ticket)) {
            return $data;
        }
        $data['all_num'] = $ticket['ticket_num'] + $ticket['consumer_num'];//总奖券数量
        $data['ticket_num'] = $ticket['ticket_num'];//剩余奖券
        $mall_log = $this->nationGridModel->getMalllog($user_id, [2, 10]);
        $data['award_score'] = array_sum(array_column($mall_log, 'gift_value')); //积分数量
        return $data;
    }


    //获取领取记录
    public function getGridLog($user_id)
    {
        return $this->nationGridModel->getInfo($user_id);
    }


    public function getGiftRank($user_id)
    {
        $key = $user_id;
        $send_rank = $this->sendGiftModel->allSendRank();
        $collect_rank = $this->sendGiftModel->allCollectRank();
        $room_rank = $this->sendGiftModel->allRoomRank();
        $room = $this->roomModel->getRoomInfoToUserId($user_id);
        $user_send_rank = $user_collect_rank = $rank_room = '未上榜';
        if (in_array($key, $send_rank)) {
            $send_rank = array_flip($send_rank);
            $user_send_rank = $send_rank[$key] + 1;
        }
        if (in_array($key, $collect_rank)) {
            $collect_rank = array_flip($collect_rank);
            $user_collect_rank = $collect_rank[$key] + 1;
        }
        if ($room) {
            $key = $room['room_id'];
            if (in_array($key, $room_rank)) {
                $room_rank = array_flip($room_rank);
                $rank_room = $room_rank[$key] + 1;
            }
        }
        return ['0' => $user_send_rank, '1' => $user_collect_rank, '2' => $rank_room];
    }

    public function getDialAwardLog($user_id)
    {
        $res = $this->nationGridModel->getLog($user_id);
        foreach ($res as &$v) {
            $time = explode(' ', $v['created_at']);
            $year = explode('-', $time[0]);
            $v['year'] = $year[0] . '-' . substr($year[1], 1) . '-' . substr($year[2], 1);
            $v['hour'] = $time[1];
        }
        return $res;
    }

    //抽奖多次领取奖品
    public function clickGridLogMall($user_id, $rank)
    {
        $log = $this->nationGridModel->getInfo($user_id);
        $code = 200;
        $msg = '成功';
        $time_limit = 7;
        $res = true;
        do {
            $config = $this->configModel->getShareGoodsConfig(6);
            $activity_date = explode(' - ', $config['nation_date']['value']);
            if (date('Y-m-d H:i:s', time()) > $activity_date[1]) {
                $code = 406;
                $msg = '活动过期';
                break;
            }
            if (!$log) {
                $code = 201;
                $msg = '暂无抽奖记录';
                break;
            }
            $all_num = $log['ticket_num'] + $log['consumer_num'] ?? 0;
            switch ($rank) {
                case 1:
                    if ($all_num < 30) {
                        $code = 203;
                        $msg = '你暂未达标，请重试';
                        break;
                    }
                    if ($log['log_one'] == 1) {
                        $code = 202;
                        $msg = '您已领取';
                        break;
                    }
                    $mall_id = 114;//气泡
                    $mall_type = 3;
                    $res = $this->nationGridModel->updateLog($user_id, 'log_one');
                    if ($res) {
                        $this->audioApi->sendUsedMall($user_id, $mall_id, $mall_type, $time_limit);
                    }
                    break;
                case 2:
                    if ($all_num < 80) {
                        $code = 203;
                        $msg = '你暂未达标，请重试';
                        break;
                    }
                    if ($log['log_two'] == 1) {
                        $code = 202;
                        $msg = '您已领取';
                        break;
                    }
                    $mall_id = 113;//头饰
                    $mall_type = 1;
                    $res = $this->nationGridModel->updateLog($user_id, 'log_two');
                    if ($res) {
                        $this->audioApi->sendUsedMall($user_id, $mall_id, $mall_type, $time_limit);
                    }
                    break;
                case 3:
                    if ($all_num < 200) {
                        $code = 203;
                        $msg = '你暂未达标，请重试';
                        break;
                    }
                    if ($log['log_three'] == 1) {
                        $code = 202;
                        $msg = '您已领取';
                        break;
                    }
                    $mall_id = 54;//勋章
                    $res = $this->nationGridModel->updateLog($user_id, 'log_three');
                    if ($res) {
                        $this->audioApi->sendMedal($user_id, $mall_id, $time_limit);
                    }
                    break;
            }
        } while (false);
        if (!$res) {
            $code = 203;
            $msg = '领取失败';
        }
        return ['code' => $code, 'msg' => $msg];

    }


    //获取抽奖获得奖品
    protected function getGridAward($radio_arr, $num = 1, $user_id)
    {
        if (array_sum($radio_arr) != 1 || $num < 1) {
            return [];
        }
        $liang_type = 4;
        $gift_arr = [];
        for ($i = 0; $i < $num; $i++) {
            $rand = aliasMethodRand($radio_arr);
            if ($rand == $liang_type) {
                //判断用户是否多次抽中
                $rand = 10;
                $user_cache = $this->nationGridModel->getUserIncLiang($user_id);
                if ($user_cache < 4) {
                    $all_cache = $this->nationGridModel->getAllIncLiang();
                    if ($all_cache < 101) {
                        $rand = 6;
                    }
                }
            }
            $gift_arr[$i] = $rand;
        }
        return $gift_arr;
    }

    public function getRankCache()
    {
        $res['send'] = $this->sendGiftModel->allSendRank();
        $res['collect'] = $this->sendGiftModel->allCollectRank();
        $res['room'] = $this->sendGiftModel->allRoomRank();
        return $res;
    }

    public function awardGift()
    {
        $allSendRank = $this->sendGiftModel->allSendRank();
        $this->giftAwardToUserId($allSendRank);
        $allCollectRank = $this->sendGiftModel->allCollectRank();
        $this->giftAwardToUserId($allCollectRank);
        return true;
    }

    //发放奖励   1：头饰 2：座驾  3：气泡
    public function giftAwardToUserId($user_ids)
    {
        $config = $this->configModel->getShareGoodsConfig(6);
        $moon_bubble_id = explode(',', $config['nation_bubble_rank_id']['value']);
        $moon_headwear_ids = explode(',', $config['nation_headwear_rank_id']['value']);
        $moon_mount_ids = explode(',', $config['nation_mount_rank_id']['value']);
        $moon_medal_rank_id = 54;
        $moon_card_rank_id = 112;
        foreach ($user_ids as $k => $val) {
            $user_id = $val;
            switch ($k) {
                case 0;//第一名
                    $this->audioApi->sendMedal($user_id, $moon_medal_rank_id, 0); //勋章
                    $this->audioApi->sendUsedMall($user_id, $moon_headwear_ids[0], 1, 30);//头饰
                    $this->audioApi->sendUsedMall($user_id, $moon_mount_ids[0], 2, 15); //座驾
                    $this->audioApi->sendUsedMall($user_id, $moon_bubble_id[0], 3, 30);//气泡
                    break;
                case 1://第二名
                    $this->audioApi->sendMedal($user_id, $moon_medal_rank_id, 0); //勋章
                    $this->audioApi->sendUsedMall($user_id, $moon_headwear_ids[1], 1, 15);//头饰
                    $this->audioApi->sendUsedMall($user_id, $moon_mount_ids[1], 2, 15); //座驾
                    $this->audioApi->sendUsedMall($user_id, $moon_bubble_id[1], 3, 15);//气泡
                    break;
                case 2://第三名
                    $this->audioApi->sendMedal($user_id, $moon_medal_rank_id, 0); //勋章
                    $this->audioApi->sendUsedMall($user_id, $moon_headwear_ids[1], 1, 7);//头饰
                    $this->audioApi->sendUsedMall($user_id, $moon_mount_ids[2], 2, 15); //座驾
                    $this->audioApi->sendUsedMall($user_id, $moon_bubble_id[1], 3, 7);//气泡
                    break;
                case 3://第四名
                    $this->audioApi->sendMedal($user_id, $moon_medal_rank_id, 7); //勋章
                    $this->audioApi->sendUsedMall($user_id, $moon_headwear_ids[1], 1, 7);//头饰
                    $this->audioApi->sendUsedMall($user_id, $moon_bubble_id[1], 3, 7);//气泡
                    $this->audioApi->sendUsedMall($user_id, $moon_card_rank_id, 4, 7);//名片
                    break;
                case 4://第五名
                    $this->audioApi->sendUsedMall($user_id, $moon_headwear_ids[1], 1, 7);//头饰
                    $this->audioApi->sendUsedMall($user_id, $moon_bubble_id[1], 3, 7);//气泡
                    $this->audioApi->sendUsedMall($user_id, $moon_card_rank_id, 4, 7);//名片
                    break;
                default:

            }
            $this->audioApi->sendMsgApi($user_id, '普天同庆——"国庆奖励已发放，您可到个性装扮查收礼品哦~');
        }
        return true;
    }

    public function scoreToUser()
    {
        return $this->rechargeModel->scoreToUser();
    }

    public function rechargeDiamondToScore()
    {
        $msg = '国庆活动积分发放';
        $data = $this->rechargeModel->getUserScoreLog();
        if(!$data){
            return false;
        }
        foreach($data as $v){
            $status = $this->audioApi->diamond_recharge($v['user_id'],$v['score'],$msg);
            if($status){
                 $this->rechargeModel->updateUserScoreLog($v['id']);
            }
        }
        return true;
    }


}